package com.wzdigit.wms.wms.service.mid;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wzdigit.framework.Shift;
import com.wzdigit.mes.basic.api.BasicDataItemDetailApi;
import com.wzdigit.mes.basic.api.BasicGetCustomerApi;
import com.wzdigit.mes.basic.api.BasicSimpleCodeRuleApi;
import com.wzdigit.mes.basic.dto.CustomerDto;
import com.wzdigit.mes.basic.dto.DataItemDetailDTO;
import com.wzdigit.wms.basic.domain.asn.AsnDtl;
import com.wzdigit.wms.basic.domain.asn.AsnHeader;
import com.wzdigit.wms.basic.domain.basic.Sku;
import com.wzdigit.wms.basic.domain.basic.Warehouse;
import com.wzdigit.wms.basic.domain.inventory.Lotno;
import com.wzdigit.wms.basic.domain.mapping.CodeMapping;
import com.wzdigit.wms.basic.domain.mid.IfCompDeliveryDtl;
import com.wzdigit.wms.basic.domain.mid.IfCompDeliveryHeader;
import com.wzdigit.wms.basic.domain.mid.JobSyncErrorData;
import com.wzdigit.wms.basic.domain.mid.dtos.IfCompDeliveryHeaderDto;
import com.wzdigit.wms.basic.domain.transfer.GdnDtl;
import com.wzdigit.wms.basic.domain.transfer.GdnHeader;
import com.wzdigit.wms.basic.enums.*;
import com.wzdigit.wms.basic.mapper.core.basic.SkuMapper;
import com.wzdigit.wms.basic.mapper.core.basic.WarehouseMapper;
import com.wzdigit.wms.basic.mapper.core.mid.IfCompDeliveryDtlMapper;
import com.wzdigit.wms.basic.mapper.core.mid.IfCompDeliveryHeaderMapper;
import com.wzdigit.wms.basic.mapper.core.mid.JobSyncErrorDataMapper;
import com.wzdigit.wms.basic.mapper.core.order.AsnDtlMapper;
import com.wzdigit.wms.basic.mapper.core.order.AsnHeaderMapper;
import com.wzdigit.wms.basic.mapper.core.transfer.GdnDtlMapper;
import com.wzdigit.wms.basic.mapper.core.transfer.GdnHeaderMapper;
import com.wzdigit.wms.basic.util.StringUtils;
import com.wzdigit.wms.wms.constant.SyncTypeConstant;
import com.wzdigit.wms.wms.exception.SyncException;
import com.wzdigit.wms.basic.service.code.LotnoService;
import com.wzdigit.wms.wms.service.mapping.CodeMappingNewService;
import lombok.extern.slf4j.Slf4j;
import net.dreamlu.mica.core.utils.BeanUtil;
import net.dreamlu.mica.core.utils.CollectionUtil;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 接口中间表-公司间交货表头 服务实现类
 * </p>
 *
 * @author
 * @since 2021-03-17
 */
@Service
@Slf4j
public class IfCompDeliveryService {

    @Autowired
    IfCompDeliveryHeaderMapper ifCompDeliveryHeaderMapper;

    @Autowired
    IfCompDeliveryDtlMapper ifCompDeliveryDtlMapper;

    @Autowired
    GdnHeaderMapper gdnHeaderMapper;

    @Autowired
    AsnHeaderMapper asnHeaderMapper;

    @Autowired
    GdnDtlMapper gdnDtlMapper;

    @Autowired
    AsnDtlMapper asnDtlMapper;

    @Autowired
    SkuMapper skuMapper;

    @Autowired
    WarehouseMapper warehouseMapper;

    @Autowired
    LotnoService lotnoService;

    @Autowired
    SyncErrorDataService syncErrorDataService;

    @Autowired
    JobSyncErrorDataMapper jobSyncErrorDataMapper;

    @Autowired
    CodeMappingNewService codeMappingNewService;

    @Reference(version = "${dubbo.consumers.basic.simple.code.rule.api:1.0.0}", check = false)
    BasicSimpleCodeRuleApi basicSimpleCodeRuleApi;

    @Reference(version = "${dubbo.providers.basic.customer.api.version:1.0.0}", check = false, timeout = 300000)
    private BasicGetCustomerApi basicGetCustomerApi;

    @Reference(version = "${dubbo.consumers.basic.data.item.detail.api:1.0.0}", check = false, timeout = 300000)
    private BasicDataItemDetailApi dataItemDetailApi;

    @Value("${order_code.gdn.code}")
    private String gdnNoRule;

    @Value("${order_code.asn.code}")
    private String asnNoRule;

    @Value(value = "${default_owner.code}")
    private String DEFAULT_OWNER;

    @Value(value = "${default_owner.name}")
    private String DEFAULT_OWNER_NAME;

    private final String syncType = SyncTypeConstant.IF_COMP_DELIVERY_HANDLE;


    private static final String ZC23_STR = "ZC23";

    private static final String ZC24_STR = "ZC24";

    private static final String factory5500 = "5500";

    private static final String factory5520 = "5520";

    private static final String ASN_R09 = "ASN_R09";
    private static final String ASN_R10 = "ASN_R10";
    private static final String GDN_D09 = "GDN_D09";
    private static final String GDN_D10 = "GDN_D10";

    public List<IfCompDeliveryHeaderDto> selectSyncData() {

        List<IfCompDeliveryHeaderDto> ifCompDeliveryHeaderDtoList = new ArrayList<>();

        //查询中间表的主表
        //创建类型的条件构造器
        QueryWrapper<IfCompDeliveryHeader> wrapper = new QueryWrapper<>();
        wrapper.eq("ATTRIBUTE10", "0").or().isNull("ATTRIBUTE10");//0 或者null表示未同步到业务表
       // wrapper.eq("BILL_NO","8001546471");
//        wrapper.orderByAsc("ifCompDeliveryHeaderId");//根据主键正序排序

        String lastSql = "and ROWNUM <= $ order by IF_COMP_DELIVERY_HEADER_ID asc";
        lastSql = lastSql.replaceAll("\\$", "5000");//设置最大查询个数,TODO 后面改到配置文件
        wrapper.last(lastSql);

        List<IfCompDeliveryHeader> selectSyncData = ifCompDeliveryHeaderMapper.selectList(wrapper);

        if (!CollectionUtil.isEmpty(selectSyncData)) {
            //遍历主表，查询其明细
            for (IfCompDeliveryHeader ifCompDeliveryHeader : selectSyncData) {
                IfCompDeliveryHeaderDto ifCompDeliveryHeaderDto = new IfCompDeliveryHeaderDto();

                BeanUtil.copyNonNull(ifCompDeliveryHeader, ifCompDeliveryHeaderDto);

                //构造查询条件，根据明细表对应的主表ID查询
                QueryWrapper<IfCompDeliveryDtl> queryWrapper = new QueryWrapper<>(new IfCompDeliveryDtl().setIfCompDeliveryHeaderId(ifCompDeliveryHeader.getIfCompDeliveryHeaderId()));
                List<IfCompDeliveryDtl> ifTransferDtls = ifCompDeliveryDtlMapper.selectList(queryWrapper);

                ifCompDeliveryHeaderDto.setIfCompDeliveryDtlList(ifTransferDtls);
                ifCompDeliveryHeaderDtoList.add(ifCompDeliveryHeaderDto);
            }
        }
        return ifCompDeliveryHeaderDtoList;
    }

    /**
     * 中间表转业务表
     *
     * @param data
     */
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
    public void syncData(IfCompDeliveryHeaderDto data) throws SyncException {
        //根据明细字段确定是成品还是原材料
        List<IfCompDeliveryDtl> list = data.getIfCompDeliveryDtlList();
        if(list==null||list.size()==0){
            throw new SyncException(syncType, data.getBillNo(), "没有明细行");
        }
        IfCompDeliveryDtl ifCompDeliveryDtl = list.get(0);
        if(StringUtils.isNotNull(ifCompDeliveryDtl.getSoNo())){//成品
            syncDataToWms(data);
        }else{//原材料
            syncDataToRaw(data);
        }

    }



    private void syncDataToWms(IfCompDeliveryHeaderDto data) throws SyncException {
        String billNo = data.getBillNo();//单据号

        String billType = data.getBillType();//业务类型--单据类型
        String betweenCompaniesOutCode = GdnTypeEnum.D09.getCode();//D09
        String betweenCompaniesReturnCode = GdnTypeEnum.D10.getCode();//D10


        String r10Code = AsnTypeEnum.R10.getCode();//R10
        String r09Code = AsnTypeEnum.R09.getCode();//R09

        List<IfCompDeliveryDtl> ifCompDeliveryDtlList = data.getIfCompDeliveryDtlList();


        IfCompDeliveryDtl deliveryDtl = new IfCompDeliveryDtl();
        if (ifCompDeliveryDtlList.size()>0) {
            deliveryDtl = ifCompDeliveryDtlList.get(0);
        }

        String fmFactory = deliveryDtl.getFmFactory();
        String toFactory = deliveryDtl.getToFactory();

        boolean flagFm = fmFactory != null && (fmFactory.equals(factory5500) || fmFactory.equals(factory5520));

        boolean flagTo = toFactory != null && (toFactory.equals(factory5500) || toFactory.equals(factory5520));

        if ((billType.equals(ZC23_STR) && flagFm) || (billType.equals(ZC24_STR) && flagTo)) {
            //发货单逻辑

            //走发货单逻辑
            GdnHeader gdnHeader = new GdnHeader();

            String gdnRule = "";
            if (billType.equals(ZC23_STR) && flagFm) {
                gdnHeader.setGdnType(betweenCompaniesOutCode);
                gdnRule = GDN_D09;
            } else if (billType.equals(ZC24_STR) && flagTo) {
                gdnHeader.setGdnType(betweenCompaniesReturnCode);
                gdnRule = GDN_D10;
            }

            //生成【发货单单号】
            String gdnNo = null;
            try {
                gdnNo = this.basicSimpleCodeRuleApi.GenerateCode(gdnRule);
            } catch (Exception e) {
                throw new SyncException(syncType, billNo, "生成发货单号失败" + (e == null ? e : e.getMessage()));
            }
            gdnHeader.setGdnNo(gdnNo);
            gdnHeader.setSourceShipNotice(billNo);
            gdnHeader.setShipDate(data.getDeliveryTime());

            CustomerDto customer = basicGetCustomerApi.getCustomerByCode(data.getCustomerCode());
            if (customer != null) {
                gdnHeader.setCustomer(customer.getCustomerId());
            }

            gdnHeader.setGdnRef1(data.getCustomerName());
            gdnHeader.setGdnCreateUser(data.getCreateUser());
            gdnHeader.setGdnCreateTime(data.getCreateTime());
            gdnHeader.setComments(data.getRemarks());

//            gdnHeader.setGdnRef3(data.getAttribute1());
            gdnHeader.setGdnRef3(billType);

            gdnHeader.setStatus(GdnHeaderStatusEnum.CREATE.getCode());//新建状态
//            gdnHeader.setGdnType(billType);//类型

            gdnHeader.setFactory(fmFactory);//工厂
            gdnHeader.setGdnRef2(deliveryDtl.getToFactory());//收货工厂

            //默认货主，查字典
            DataItemDetailDTO dataItem = dataItemDetailApi.getDefaultDataItemDetailByCode(DEFAULT_OWNER);
            gdnHeader.setOwner(dataItem == null || dataItem.getItemValue() == null ? 0 : Integer.parseInt(dataItem.getItemValue()));


            //更新OR新增
            //根据：来源发货通知单 查询
            QueryWrapper<GdnHeader> queryWrapper = new QueryWrapper<>(new GdnHeader().setSourceShipNotice(billNo));
            GdnHeader headerRecord = gdnHeaderMapper.selectOne(queryWrapper);
            if (headerRecord == null) {
                gdnHeader.setCreateTime(new Date());
                gdnHeader.setCreateUser("system");
                gdnHeaderMapper.insert(gdnHeader);
            } else {
                gdnHeader.setGdnNo(headerRecord.getGdnNo());
                gdnHeader.setGdnHeaderId(headerRecord.getGdnHeaderId());
                gdnHeader.setModifyTime(new Date());
                gdnHeader.setModifyUser("system");
                gdnHeaderMapper.updateById(gdnHeader);
            }

            int line = 1;

//            List<GdnDtl> gdnDtlList = gdnDtlMapper.selectByHeaderId(gdnHeader.getGdnHeaderId());
//
//            if (gdnDtlList.size()> 0) {
//                gdnDtlList = gdnDtlList.stream().sorted(Comparator.comparing(GdnDtl::getGdnLineNo).reversed()).collect(Collectors.toList());
//                line = gdnDtlList.get(0).getGdnLineNo();
//            }



            //操作明细
            for (IfCompDeliveryDtl ifCompDeliveryDtl : ifCompDeliveryDtlList) {
                GdnDtl gdnDtl = new GdnDtl();

                gdnDtl.setSourceShipNotice(ifCompDeliveryDtl.getBillNo());

                gdnDtl.setGdnHeaderId(gdnHeader.getGdnHeaderId());
                gdnDtl.setGdnLineNo(line);
                gdnDtl.setSourceShipNoticeItem(ifCompDeliveryDtl.getBillLineNo());
                gdnDtl.setRequireTime(ifCompDeliveryDtl.getDeliveryTime());

                //TODO 测试生产注释回来
                Sku sku = skuMapper.selectOne(new QueryWrapper<>(new Sku().setSkuCode(ifCompDeliveryDtl.getMaterialCode())));
                if (sku == null) {
                    throw new SyncException(syncType, billNo, "物料" + ifCompDeliveryDtl.getMaterialCode() + "不存在");
                }
                //TODO 测试生产注释回来
                gdnDtl.setSkuId(sku.getSkuId());
//                gdnDtl.setSkuId(1);
                gdnDtl.setOrderQty(ifCompDeliveryDtl.getQty());


                //TODO 测试生产注释回来
                if (ifCompDeliveryDtl.getFmWhs() != null && ifCompDeliveryDtl.getFmFactory() != null) {
                    CodeMapping codeMapping = codeMappingNewService.selectByMappingCodeAndFacCodeAnyOne(ifCompDeliveryDtl.getFmWhs(), ifCompDeliveryDtl.getFmFactory());
                    if (codeMapping!=null) {
                        Warehouse warehouse = warehouseMapper.selectOneByWhsCode(codeMapping.getWmsCode());
                        if (warehouse != null) {
                            gdnDtl.setWhsId(warehouse.getWhsId());
                        } else {
                            gdnDtl.setWhsId(null);
                        }
                    } else {
                        gdnDtl.setWhsId(null);
                    }
                }
//                gdnDtl.setWhsId(Integer.valueOf(ifCompDeliveryDtl.getFmWhs()));
//                gdnDtl.setWhsId(1);

                gdnDtl.setPoNo(ifCompDeliveryDtl.getPoNo());
                gdnDtl.setPoLineNo(ifCompDeliveryDtl.getPoLineNo());

                gdnDtl.setSoNo(ifCompDeliveryDtl.getSoNo());
                gdnDtl.setSoLineNo(ifCompDeliveryDtl.getSoLineNo());
                gdnDtl.setComments(ifCompDeliveryDtl.getRemarks());


                gdnDtl.setQaStatus(1);
                gdnDtl.setStatus(GdnDtlStatusEnum.CREATE.getCode());
                //TODO 测试生产注释回来
                gdnDtl.setUnit(sku.getBaseUnit());
//                gdnDtl.setUnit("sku.getBaseUnit()");
                //订单号（PO）与PO_NO不一样(通不过备用字段Attribute4传过来) by luxm
                gdnDtl.setGdnRef1(ifCompDeliveryDtl.getAttribute4());

                QueryWrapper<GdnDtl> gdnDtlQueryWrapper = new QueryWrapper<>(new GdnDtl().setGdnHeaderId(gdnHeader.getGdnHeaderId()).setSourceShipNoticeItem(ifCompDeliveryDtl.getBillLineNo()));
                GdnDtl dtlRecord = gdnDtlMapper.selectOne(gdnDtlQueryWrapper);


                if (dtlRecord == null) {
                    gdnDtl.setCreateTime(new Date());
                    gdnDtl.setCreateUser("system");
                    try {
                        gdnDtlMapper.insert(gdnDtl);
                    } catch (Exception e) {
                        e.printStackTrace();
                        throw new SyncException(syncType, billNo, "发货单明细行新增失败：" + e.getMessage());
                    }
                } else {
                    gdnDtl.setGdnDtlId(dtlRecord.getGdnDtlId());
                    gdnDtl.setModifyTime(new Date());
                    gdnDtl.setModifyUser("system");
                    try {
                        gdnDtlMapper.updateById(gdnDtl);
                    } catch (Exception e) {
                        throw new SyncException(syncType, billNo, "发货单明细行修改失败：" + e.getMessage());
                    }
                }


                //生成批次号
                Lotno lotno = lotnoService.getLotnoByGdnDtlId(gdnDtl.getGdnDtlId());

                if (lotno != null) {
                    log.info("发货单明细：批次号{},ID{}", lotno.getLotno(), gdnDtl.getGdnDtlId());
                    gdnDtl.setLotno(lotno.getLotno());

                    int r = 1;
                    try {
                        r = setLotAttr(gdnDtl, lotno);
                    } catch (Exception e) {
                        e.printStackTrace();
                        throw new SyncException(syncType, billNo, "设置批次属性" + r + "失败");
                    }
                    gdnDtlMapper.updateLotNoAndAttr(gdnDtl);
                }
                line++;
            }
        } else if ((billType.equals(ZC23_STR) && flagTo) || (billType.equals(ZC24_STR) && flagFm)) {
            //收货单逻辑

            //走收货单逻辑
            AsnHeader asnHeader = new AsnHeader();

            String asnRule = "";
            if (billType.equals(ZC23_STR) && flagTo) {
                asnHeader.setAsnType(r09Code);
                asnRule=ASN_R09;
            } else if (billType.equals(ZC24_STR) && flagFm) {
                asnHeader.setAsnType(r10Code);
                asnRule=ASN_R10;
            }

            String asnNo = null;
            try {
                asnNo = this.basicSimpleCodeRuleApi.GenerateCode(asnRule);
            } catch (Exception e) {
                throw new SyncException(syncType, billNo, "生成发货单号失败" + (e == null ? e : e.getMessage()));
            }
            asnHeader.setAsnNo(asnNo);

            asnHeader.setSourceNo(billNo);
            asnHeader.setActualarrivetime(data.getDeliveryTime());

            CustomerDto customer = basicGetCustomerApi.getCustomerByCode(data.getCustomerCode());
            if (customer != null) {
                asnHeader.setCustomerId(customer.getCustomerId());
            }
            asnHeader.setAsnRef1(data.getCustomerName());
            asnHeader.setAsnCreateUser(data.getCreateUser());
            asnHeader.setAsnCreateTime(data.getCreateTime());
            asnHeader.setComments(data.getRemarks());

            asnHeader.setAsnRef3(billType);

            asnHeader.setStatus(AsnHeaderStatusEnum.CREATE.getCode());
            asnHeader.setQcStatus(QcStatusEnum.已创建质检单.getCode());
            asnHeader.setReleaseStatus(1);//已释放

            //todo 设置仓库，工厂
//            asnHeader.setToWhs();
            asnHeader.setFactory(toFactory);
            asnHeader.setAsnRef2(deliveryDtl.getFmFactory());//发货工厂


            //默认货主，查字典
            DataItemDetailDTO dataItem = dataItemDetailApi.getDefaultDataItemDetailByCode(DEFAULT_OWNER);
            asnHeader.setOwner(dataItem == null || dataItem.getItemValue() == null ? 0 : Integer.parseInt(dataItem.getItemValue()));

            //更新OR新增
            //根据：来源发货通知单 查询
//            QueryWrapper<GdnHeader> queryWrapper = new QueryWrapper<>(new GdnHeader().setSourceShipNotice(billNo));
//            GdnHeader headerRecord = gdnHeaderMapper.selectOne(queryWrapper);

            //创建类型的条件构造器
            QueryWrapper<AsnHeader> queryWrapper = new QueryWrapper<>(new AsnHeader().setSourceNo(billNo));
            AsnHeader headerRecord = asnHeaderMapper.selectOne(queryWrapper);


            if (headerRecord == null) {
                asnHeader.setCreateTime(new Date());
                asnHeader.setCreateUser("system");
                asnHeaderMapper.insert(asnHeader);
            } else {
//                asnHeader.setAsnNo(headerRecord.getGdnNo());
//                asnHeader.setAsnHeaderId(headerRecord.getGdnHeaderId());

                asnHeader.setAsnNo(headerRecord.getAsnNo());
                asnHeader.setAsnHeaderId(headerRecord.getAsnHeaderId());


                asnHeader.setModifyTime(new Date());
                asnHeader.setModifyUser("system");
                asnHeaderMapper.updateById(asnHeader);
            }
            //

            int line= 1;
            for (IfCompDeliveryDtl ifCompDeliveryDtl : ifCompDeliveryDtlList) {
                AsnDtl asnDtl = new AsnDtl();

                asnDtl.setSourceNo(ifCompDeliveryDtl.getBillNo());
                asnDtl.setAsnNo(asnHeader.getAsnNo());
                asnDtl.setAsnLineNo(String.valueOf(line));
                asnDtl.setAsnHeaderId(asnHeader.getAsnHeaderId());
                asnDtl.setSourceLineNo(ifCompDeliveryDtl.getBillLineNo());
                Sku sku = skuMapper.selectOne(new QueryWrapper<>(new Sku().setSkuCode(ifCompDeliveryDtl.getMaterialCode())));
                if (sku == null) {
                    throw new SyncException(syncType, billNo, "物料" + ifCompDeliveryDtl.getMaterialCode() + "不存在");
                }
                asnDtl.setSkuId(sku.getSkuId());
                asnDtl.setUnit(sku.getBaseUnit());
                asnDtl.setSkuDesc(sku.getDescCn());
                asnDtl.setOrderQty(ifCompDeliveryDtl.getQty());
                asnDtl.setOrderQtyEa(ifCompDeliveryDtl.getQty());//LXM说和上面取值一样

                asnDtl.setQcStatus(QcStatusEnum.未创建质检单.getCode());

                if (ifCompDeliveryDtl.getFmWhs() != null && ifCompDeliveryDtl.getToFactory() != null) {
                    CodeMapping codeMapping = codeMappingNewService.selectByMappingCodeAndFacCodeAnyOne(ifCompDeliveryDtl.getFmWhs(), ifCompDeliveryDtl.getToFactory());
                    if (codeMapping!=null) {
                        Warehouse warehouse = warehouseMapper.selectOneByWhsCode(codeMapping.getWmsCode());
                        if (warehouse != null) {
                            asnDtl.setWhsId(warehouse.getWhsId());
                        } else {
                            asnDtl.setWhsId(null);
                        }
                    } else {
                        asnDtl.setWhsId(null);
                    }
                }

                asnDtl.setPoNo(ifCompDeliveryDtl.getPoNo());
                asnDtl.setPoLineNo(ifCompDeliveryDtl.getPoLineNo());

                asnDtl.setSoNo(ifCompDeliveryDtl.getSoNo());
                asnDtl.setSoLineNo(ifCompDeliveryDtl.getSoLineNo());
                asnDtl.setComments(ifCompDeliveryDtl.getRemarks());

                //订单号（PO）与PO_NO不一样(通不过备用字段Attribute4传过来) by luxm
                asnDtl.setAsnRef1(ifCompDeliveryDtl.getAttribute4());

                //TODO ZYF:加个QA_STATUS
                asnDtl.setQaStatus(QaStatusEnum.OK.code);

                //构造条件
                //创建类型的条件构造器
                QueryWrapper<AsnDtl> wrapper = new QueryWrapper<>();
                wrapper.eq("ASN_HEADER_ID", asnHeader.getAsnHeaderId());
                wrapper.eq("SOURCE_LINE_NO", asnDtl.getSourceLineNo());

                AsnDtl dtlRecord = asnDtlMapper.selectOne(wrapper);


                if (dtlRecord == null) {
                    asnDtl.setStatus(AsnDtlStatusEnum.CREATE.getCode());//新建状态
                    asnDtl.setCreateTime(new Date());
                    asnDtl.setCreateUser("system");
                    try {
                        asnDtlMapper.insert(asnDtl);
                    } catch (Exception e) {
                        e.printStackTrace();
                        throw new SyncException(syncType, billNo, "发货单明细行新增失败：" + e.getMessage());
                    }
                } else {
                    asnDtl.setAsnDtlId(dtlRecord.getAsnDtlId());
                    asnDtl.setModifyTime(new Date());
                    asnDtl.setModifyUser("system");
                    try {
                        asnDtlMapper.updateById(asnDtl);
                    } catch (Exception e) {
                        throw new SyncException(syncType, billNo, "发货单明细行修改失败：" + e.getMessage());
                    }
                }


                //生成批次号  这个地方取ASN_REF1
                Lotno lotno = lotnoService.getLotnoByAsnDtlId(asnDtl.getAsnDtlId());

                if (lotno != null) {
                    log.info("发货单明细：批次号{},ID{}", lotno.getLotno(), asnDtl.getAsnDtlId());
                    asnDtl.setLotno(lotno.getLotno());

                    int r = 1;
                    try {
                        r = setLotAttr(asnDtl, lotno);
                    } catch (Exception e) {
                        e.printStackTrace();
                        throw new SyncException(syncType, billNo, "设置批次属性" + r + "失败");
                    }
                    asnDtlMapper.updateLotNoAndAttr(asnDtl);
                }
                line++;
            }
        }

        //更新WMS中间表的同步标识
        //1.主表
        IfCompDeliveryHeader ifCompDeliveryHeader = new IfCompDeliveryHeader();
        ifCompDeliveryHeader.setModifyTime(new Date());
        ifCompDeliveryHeader.setModifyUser("system");
        ifCompDeliveryHeader.setAttribute10("1");//已同步

        //创建类型的条件构造器
        QueryWrapper<IfCompDeliveryHeader> wrapper = new QueryWrapper<>();
        wrapper.eq("IF_COMP_DELIVERY_HEADER_ID", data.getIfCompDeliveryHeaderId());

        int row = ifCompDeliveryHeaderMapper.update(ifCompDeliveryHeader, wrapper);
        log.info("主表:成功更新{}条记录",row);
        //2.明细表
        List<Integer> ifCompDeliveryDtlIdList = ifCompDeliveryDtlList.stream().filter(s -> s.getIfCompDeliveryDtlId() != null).map(s -> s.getIfCompDeliveryDtlId()).collect(Collectors.toList());

        IfCompDeliveryDtl ifCompDeliveryDtl = new IfCompDeliveryDtl();
        ifCompDeliveryDtl.setModifyTime(new Date());
        ifCompDeliveryDtl.setModifyUser("system");
        ifCompDeliveryDtl.setAttribute10("1");//已同步

        //创建类型的条件构造器
        QueryWrapper<IfCompDeliveryDtl> wrapperDtl = new QueryWrapper<>();
        wrapperDtl.in("IF_COMP_DELIVERY_DTL_ID",ifCompDeliveryDtlIdList);
        int rowDtl = ifCompDeliveryDtlMapper.update(ifCompDeliveryDtl, wrapperDtl);
        log.info("明细:成功更新{}条记录",rowDtl);


        //检查是否有同步失败的记录，有则更新为成功
        JobSyncErrorData jobSyncErrorData = jobSyncErrorDataMapper.getSyncErrorDataByTypeAndCode(syncType, billNo);
        if (jobSyncErrorData != null && 0 == jobSyncErrorData.getIsSuccess()) {
            syncErrorDataService.updateSuccessByCode(syncType, billNo);
        }
    }


    /**
     * 设置批次属性1~12
     *
     * @param obj
     * @param lotno
     */
    private int setLotAttr(Object obj, Lotno lotno) throws Exception {
        int i = 1;
        for (i = 1; i < 13; i++) {
            try {
                Class<?> aClass = obj.getClass();
                Object value = lotno.getClass().getMethod("getLotAttr" + i).invoke(lotno, null);
                Method method = aClass.getMethod("setLotAttr" + i, String.class);
                method.invoke(obj, value);
            } catch (Exception e) {
                e.printStackTrace();
                break;
            }
        }
        return i;
    }


    private static final String ZC23_ZC24_STR = "ZC23,ZC24";//和成品共用逻辑
    private static final String upFactory = "5500,5520";//已上工厂



    private void syncDataToRaw(IfCompDeliveryHeaderDto data) throws SyncException {
        List<IfCompDeliveryDtl> ifCompDeliveryDtlList = data.getIfCompDeliveryDtlList();
        IfCompDeliveryDtl deliveryDtl = new IfCompDeliveryDtl();
        if (ifCompDeliveryDtlList.size()>0) {
            deliveryDtl = ifCompDeliveryDtlList.get(0);
        }
        String fmFactory = deliveryDtl.getFmFactory();//发货工厂
        String toFactory = deliveryDtl.getToFactory();//收货工厂 25
        if ("ZC25".equals(data.getBillType())||"ZC32".equals(data.getBillType())){//出库8000009248
            syncDataToRawGdn(data,GdnTypeEnum.D17.code,fmFactory,toFactory);
        }
        if ("ZC26".equals(data.getBillType())||"ZC33".equals(data.getBillType())){//入库 质检状态为合格
            syncDataToRawAsn(data,AsnTypeEnum.R15.code,QaStatusEnum.OK.code,QcStatusEnum.全部质检.getCode(),fmFactory,toFactory);
        }else if(data.getBillType().equals(ZC23_STR)||data.getBillType().equals(ZC24_STR)){//沿用成品的逻辑
            String billType = data.getBillType();//业务类型-
            //对应仓库有管控
            boolean flagFm = fmFactory != null && (fmFactory.equals(factory5500) || fmFactory.equals(factory5520));
            boolean flagTo = toFactory != null && (toFactory.equals(factory5500) || toFactory.equals(factory5520));
            if(billType.equals(ZC23_STR)){//公司间交货
                if(flagFm&&flagTo){//先出后入
                    syncDataToRawGdn(data,GdnTypeEnum.D26.code,fmFactory,toFactory);
                }else if(flagFm&&!flagTo){//只出不入
                    syncDataToRawGdn(data,GdnTypeEnum.D24.code,fmFactory,toFactory);
                }else if(!flagFm&&flagTo){//只入不出
                    syncDataToRawAsn(data,AsnTypeEnum.R19.code,QaStatusEnum.WAIT.code,QcStatusEnum.未创建质检单.getCode(),toFactory,fmFactory);
                }

            }else if(billType.equals(ZC24_STR)){
                if(flagFm&&flagTo){//先出后入
                    syncDataToRawGdn(data,GdnTypeEnum.D27.code,toFactory,fmFactory);
                }else if(flagFm&&!flagTo){//只入不出
                    syncDataToRawAsn(data,AsnTypeEnum.R20.code,QaStatusEnum.OK.code,QcStatusEnum.全部质检.getCode(),fmFactory,toFactory);
                }else if(!flagFm&&flagTo){//只出不入
                    syncDataToRawGdn(data,GdnTypeEnum.D25.code,toFactory,fmFactory);

                }
            }
        }
    }

    private void syncDataToRawGdn(IfCompDeliveryHeaderDto data,String gdnType,String coreFactory,String otherFactory) throws SyncException {
        String billNo = data.getBillNo();//单据号
        //查询单据是否存在//
        QueryWrapper<GdnHeader> queryWrapper = new QueryWrapper<>(new GdnHeader().setGdnNo(billNo));
        GdnHeader headerRecord = gdnHeaderMapper.selectOne(queryWrapper);
        //删除逻辑 主表字段传1
        if("2".equals(data.getAttribute1())){
            if(headerRecord==null){
                throw new SyncException(syncType, billNo,  "没有查询到单号为:" + billNo+ "的订单 ,无法删除");
            }else {
                gdnDtlMapper.delete(new QueryWrapper<GdnDtl>(new GdnDtl().setGdnHeaderId(headerRecord.getGdnHeaderId())));//删除明细
                gdnHeaderMapper.deleteById(headerRecord.getGdnHeaderId());//删除主表数据
                return ; //删除后直接跳过本条数据
            }
        }
        //获取一行明细 要从明细里面拿工厂信息......
        List<IfCompDeliveryDtl> ifCompDeliveryDtlList = data.getIfCompDeliveryDtlList();



        GdnHeader gdnHeader = new GdnHeader();
        gdnHeader.setGdnNo(billNo);
        gdnHeader.setShipDate(data.getDeliveryTime());
        gdnHeader.setGdnType(gdnType);
        CustomerDto customer = basicGetCustomerApi.getCustomerByCode(data.getCustomerCode());
        if (customer != null) {
            gdnHeader.setCustomer(customer.getCustomerId());
        }

        gdnHeader.setGdnRef1(data.getCustomerName());
        gdnHeader.setGdnCreateUser(data.getCreateUser());
        gdnHeader.setGdnCreateTime(data.getCreateTime());
        gdnHeader.setComments(data.getRemarks());
        gdnHeader.setSourceType(data.getBillType());
        gdnHeader.setStatus(GdnHeaderStatusEnum.CREATE.getCode());//新建状态
        gdnHeader.setFactory(coreFactory);
        gdnHeader.setGdnRef2(otherFactory);
        DataItemDetailDTO dataItem = dataItemDetailApi.getDefaultDataItemDetailByCode(DEFAULT_OWNER);
        gdnHeader.setOwner(dataItem == null || dataItem.getItemValue() == null ? 0 : Integer.parseInt(dataItem.getItemValue()));

        if (headerRecord == null) {
            gdnHeader.setCreateTime(new Date());
            gdnHeader.setCreateUser("system");
            gdnHeaderMapper.insert(gdnHeader);
        } else {
            gdnHeader.setGdnNo(headerRecord.getGdnNo());
            gdnHeader.setGdnHeaderId(headerRecord.getGdnHeaderId());
            gdnHeader.setModifyTime(new Date());
            gdnHeader.setModifyUser("system");
            gdnHeaderMapper.updateById(gdnHeader);
        }

        //同步明细
        Integer gdnLineNo = gdnDtlMapper.selectMaxLineId(gdnHeader.getGdnHeaderId());
        if(gdnLineNo == null){
            gdnLineNo =  0;
        }
        for (IfCompDeliveryDtl ifCompDeliveryDtl : ifCompDeliveryDtlList) {
            GdnDtl gdnDtl = new GdnDtl();
            gdnDtl.setSourceShipNotice(ifCompDeliveryDtl.getBillNo());
            gdnDtl.setGdnLineNo(++gdnLineNo);
            gdnDtl.setSourceShipNoticeItem(ifCompDeliveryDtl.getBillLineNo());
            gdnDtl.setRequireTime(ifCompDeliveryDtl.getDeliveryTime());
            Sku sku = skuMapper.selectOne(new QueryWrapper<>(new Sku().setSkuCode(ifCompDeliveryDtl.getMaterialCode())));
            if (sku == null) {
                throw new SyncException(syncType, billNo, "物料" + ifCompDeliveryDtl.getMaterialCode() + "不存在");
            }
            gdnDtl.setSkuId(sku.getSkuId());

            gdnDtl.setOrderQty(ifCompDeliveryDtl.getQty());
            if (ifCompDeliveryDtl.getFmWhs() != null && coreFactory!= null) {
                //设置品质状态
                if(ifCompDeliveryDtl.getFmWhs().indexOf("B")==-1&&ifCompDeliveryDtl.getFmWhs().indexOf("E")==-1){
                   gdnDtl.setQaStatus(QaStatusEnum.OK.code);
                }else{
                    gdnDtl.setQaStatus(QaStatusEnum.NG.code);
                }

                CodeMapping codeMapping = codeMappingNewService.selectByMappingCodeAndFacCodeAnyOne(ifCompDeliveryDtl.getFmWhs(), coreFactory);
                if (codeMapping!=null) {
                    Warehouse warehouse = warehouseMapper.selectOneByWhsCode(codeMapping.getWmsCode());
                    if(GdnTypeEnum.D27.code.equals(gdnType)||GdnTypeEnum.D25.code.equals(gdnType)){//出没有仓库直接设置为null
                            gdnDtl.setWhsId(null);
                            gdnDtl.setUdf5(warehouse.getWhsId()+"");
                    }else{//
                        if (warehouse != null) {
                            gdnDtl.setWhsId(warehouse.getWhsId());
                        } else {
                            gdnDtl.setWhsId(null);
                        }
                    }


                } else {
                    gdnDtl.setWhsId(null);
                }

            }
            gdnDtl.setPoNo(ifCompDeliveryDtl.getPoNo());
            gdnDtl.setPoLineNo(ifCompDeliveryDtl.getPoLineNo());
            gdnDtl.setSoNo(ifCompDeliveryDtl.getSoNo());
            gdnDtl.setSoLineNo(ifCompDeliveryDtl.getSoLineNo());
            gdnDtl.setComments(ifCompDeliveryDtl.getRemarks());

            gdnDtl.setStatus(GdnDtlStatusEnum.CREATE.getCode());
            //TODO 测试生产注释回来
            gdnDtl.setUnit(sku.getBaseUnit());
            //订单号（PO）与PO_NO不一样(通不过备用字段Attribute4传过来) by luxm
            gdnDtl.setGdnRef1(ifCompDeliveryDtl.getAttribute4());
            gdnDtl.setGdnHeaderId(gdnHeader.getGdnHeaderId());
            QueryWrapper<GdnDtl> gdnDtlQueryWrapper = new QueryWrapper<>(new GdnDtl().setGdnHeaderId(gdnHeader.getGdnHeaderId()).setSourceShipNoticeItem(ifCompDeliveryDtl.getBillLineNo()));
            GdnDtl dtlRecord = gdnDtlMapper.selectOne(gdnDtlQueryWrapper);
            if (dtlRecord == null) {
                gdnDtl.setCreateTime(new Date());
                gdnDtl.setCreateUser("system");
                try {
                    gdnDtlMapper.insert(gdnDtl);
                } catch (Exception e) {
                    e.printStackTrace();
                    throw new SyncException(syncType, billNo, "发货单明细行新增失败：" + e.getMessage());
                }
            } else {
                gdnDtl.setGdnDtlId(dtlRecord.getGdnDtlId());
                gdnDtl.setModifyTime(new Date());
                gdnDtl.setModifyUser("system");
                try {
                    gdnDtlMapper.updateById(gdnDtl);
                } catch (Exception e) {
                    throw new SyncException(syncType, billNo, "发货单明细行修改失败：" + e.getMessage());
                }
            }
        }
        //更新WMS中间表的同步标识
        //1.主表
        IfCompDeliveryHeader ifCompDeliveryHeader = new IfCompDeliveryHeader();
        ifCompDeliveryHeader.setModifyTime(new Date());
        ifCompDeliveryHeader.setModifyUser("system");
        ifCompDeliveryHeader.setAttribute10("1");//已同步

        //创建类型的条件构造器
        QueryWrapper<IfCompDeliveryHeader> wrapper = new QueryWrapper<>();
        wrapper.eq("IF_COMP_DELIVERY_HEADER_ID", data.getIfCompDeliveryHeaderId());

        int row = ifCompDeliveryHeaderMapper.update(ifCompDeliveryHeader, wrapper);
        log.info("主表:成功更新{}条记录",row);
        //2.明细表
        List<Integer> ifCompDeliveryDtlIdList = ifCompDeliveryDtlList.stream().filter(s -> s.getIfCompDeliveryDtlId() != null).map(s -> s.getIfCompDeliveryDtlId()).collect(Collectors.toList());

        IfCompDeliveryDtl ifCompDeliveryDtl = new IfCompDeliveryDtl();
        ifCompDeliveryDtl.setModifyTime(new Date());
        ifCompDeliveryDtl.setModifyUser("system");
        ifCompDeliveryDtl.setAttribute10("1");//已同步

        //创建类型的条件构造器
        QueryWrapper<IfCompDeliveryDtl> wrapperDtl = new QueryWrapper<>();
        wrapperDtl.in("IF_COMP_DELIVERY_DTL_ID",ifCompDeliveryDtlIdList);
        int rowDtl = ifCompDeliveryDtlMapper.update(ifCompDeliveryDtl, wrapperDtl);
        log.info("明细:成功更新{}条记录",rowDtl);


        //检查是否有同步失败的记录，有则更新为成功
        JobSyncErrorData jobSyncErrorData = jobSyncErrorDataMapper.getSyncErrorDataByTypeAndCode(syncType, billNo);
        if (jobSyncErrorData != null && 0 == jobSyncErrorData.getIsSuccess()) {
            syncErrorDataService.updateSuccessByCode(syncType, billNo);
        }

    }
    /**
        * @param data
    	* @param asnType
    	* @param qaStatus
    	* @param qcStatus
    	* @param coreFactory 发出工厂
    	* @param otherFactory 收货工厂
        * @return void
        * @author shuaic
        * @date 2021/8/15 20:36
    */
    private void syncDataToRawAsn(IfCompDeliveryHeaderDto data,String asnType,Integer qaStatus,Integer qcStatus,String coreFactory,String otherFactory) throws SyncException {
        //查询订单
        String billNo = data.getBillNo();
        QueryWrapper<AsnHeader> queryWrapper = new QueryWrapper<>(new AsnHeader().setAsnNo(billNo));
        AsnHeader headerRecord = asnHeaderMapper.selectOne(queryWrapper);
        //删除逻辑 主表字段传1
        if("2".equals(data.getAttribute1())){
            if(headerRecord==null){
                throw new SyncException(syncType, billNo,  "没有查询到单号为:" + billNo+ "的订单 ,无法删除");
            }else {
                asnDtlMapper.delete(new QueryWrapper<AsnDtl>(new AsnDtl().setAsnHeaderId(headerRecord.getAsnHeaderId())));//删除明细
                asnHeaderMapper.deleteById(headerRecord.getAsnHeaderId());//删除主表数据
                return ; //删除后直接跳过本条数据
            }
        }
        List<IfCompDeliveryDtl> ifCompDeliveryDtlList = data.getIfCompDeliveryDtlList();
        IfCompDeliveryDtl deliveryDtl = new IfCompDeliveryDtl();
        if (ifCompDeliveryDtlList.size()>0) {
            deliveryDtl = ifCompDeliveryDtlList.get(0);
        }else {
            Shift.fatal("同步送货单失败,没有明细行");
        }

        AsnHeader asnHeader = new AsnHeader();
        asnHeader.setAsnType(asnType);
        asnHeader.setAsnNo(billNo);
        asnHeader.setActualarrivetime(data.getDeliveryTime());
        CustomerDto customer = basicGetCustomerApi.getCustomerByCode(data.getCustomerCode());
        if (customer != null) {
            asnHeader.setCustomerId(customer.getCustomerId());
        }
        asnHeader.setAsnRef1(data.getCustomerName());
        asnHeader.setAsnCreateUser(data.getCreateUser());
        asnHeader.setAsnCreateTime(data.getCreateTime());
        asnHeader.setComments(data.getRemarks());

        asnHeader.setAsnRef3(data.getBillType());

        asnHeader.setStatus(AsnHeaderStatusEnum.CREATE.code);
        asnHeader.setQcStatus(qcStatus);
        asnHeader.setReleaseStatus(1);//已释放

        //todo 设置仓库，工厂
//            asnHeader.setToWhs();
        asnHeader.setFactory(coreFactory);
        asnHeader.setAsnRef2(otherFactory);
        //默认货主，查字典
        DataItemDetailDTO dataItem = dataItemDetailApi.getDefaultDataItemDetailByCode(DEFAULT_OWNER);
        asnHeader.setOwner(dataItem == null || dataItem.getItemValue() == null ? 0 : Integer.parseInt(dataItem.getItemValue()));



        if (headerRecord == null) {
            asnHeader.setCreateTime(new Date());
            asnHeader.setCreateUser("system");
            asnHeaderMapper.insert(asnHeader);
        } else {
//                asnHeader.setAsnNo(headerRecord.getGdnNo());
//                asnHeader.setAsnHeaderId(headerRecord.getGdnHeaderId());

            asnHeader.setAsnNo(headerRecord.getAsnNo());
            asnHeader.setAsnHeaderId(headerRecord.getAsnHeaderId());


            asnHeader.setModifyTime(new Date());
            asnHeader.setModifyUser("system");
            asnHeaderMapper.updateById(asnHeader);
        }

        for (IfCompDeliveryDtl ifCompDeliveryDtl : ifCompDeliveryDtlList) {
            AsnDtl asnDtl = new AsnDtl();

            asnDtl.setSourceNo(ifCompDeliveryDtl.getBillNo());
            asnDtl.setAsnNo(asnHeader.getAsnNo());
            asnDtl.setAsnLineNo(ifCompDeliveryDtl.getBillLineNo());
            asnDtl.setAsnHeaderId(asnHeader.getAsnHeaderId());

            Sku sku = skuMapper.selectOne(new QueryWrapper<>(new Sku().setSkuCode(ifCompDeliveryDtl.getMaterialCode())));
            if (sku == null) {
                throw new SyncException(syncType, billNo, "物料" + ifCompDeliveryDtl.getMaterialCode() + "不存在");
            }
            asnDtl.setSkuId(sku.getSkuId());
            asnDtl.setUnit(sku.getBaseUnit());
            asnDtl.setSkuDesc(sku.getDescCn());
            asnDtl.setOrderQty(ifCompDeliveryDtl.getQty());
            asnDtl.setOrderQtyEa(ifCompDeliveryDtl.getQty());//LXM说和上面取值一样
            asnDtl.setQaStatus(qaStatus);
            asnDtl.setQcStatus(qcStatus);


            if (ifCompDeliveryDtl.getFmWhs() != null && coreFactory != null) {
                CodeMapping codeMapping = codeMappingNewService.selectByMappingCodeAndFacCodeAnyOne(ifCompDeliveryDtl.getFmWhs(), coreFactory);
                if (codeMapping!=null) {
                    Warehouse warehouse = warehouseMapper.selectOneByWhsCode(codeMapping.getWmsCode());
                    if (warehouse != null) {
                        asnDtl.setWhsId(warehouse.getWhsId());
                    } else {
                        asnDtl.setWhsId(null);
                    }
                } else {
                    asnDtl.setWhsId(null);
                }
            }
            asnDtl.setPoNo(ifCompDeliveryDtl.getPoNo());
            asnDtl.setPoLineNo(ifCompDeliveryDtl.getPoLineNo());

            asnDtl.setSoNo(ifCompDeliveryDtl.getSoNo());
            asnDtl.setSoLineNo(ifCompDeliveryDtl.getSoLineNo());
            asnDtl.setComments(ifCompDeliveryDtl.getRemarks());
//订单号（PO）与PO_NO不一样(通不过备用字段Attribute4传过来) by luxm
            asnDtl.setAsnRef1(ifCompDeliveryDtl.getAttribute4());

            //构造条件
            //创建类型的条件构造器
            QueryWrapper<AsnDtl> wrapper = new QueryWrapper<>();
            wrapper.eq("ASN_HEADER_ID", asnHeader.getAsnHeaderId());
            wrapper.eq("ASN_LINE_NO", asnDtl.getAsnLineNo());

            AsnDtl dtlRecord = asnDtlMapper.selectOne(wrapper);
            if (dtlRecord == null) {
                asnDtl.setStatus(AsnDtlStatusEnum.CREATE.getCode());//新建状态
                asnDtl.setCreateTime(new Date());
                asnDtl.setCreateUser("system");
                try {
                    asnDtlMapper.insert(asnDtl);
                } catch (Exception e) {
                    e.printStackTrace();
                    throw new SyncException(syncType, billNo, "发货单明细行新增失败：" + e.getMessage());
                }
            } else {
                asnDtl.setAsnDtlId(dtlRecord.getAsnDtlId());
                asnDtl.setModifyTime(new Date());
                asnDtl.setModifyUser("system");
                try {
                    asnDtlMapper.updateById(asnDtl);
                } catch (Exception e) {
                    throw new SyncException(syncType, billNo, "发货单明细行修改失败：" + e.getMessage());
                }
            }


        }

        //更新WMS中间表的同步标识
        //1.主表
        IfCompDeliveryHeader ifCompDeliveryHeader = new IfCompDeliveryHeader();
        ifCompDeliveryHeader.setModifyTime(new Date());
        ifCompDeliveryHeader.setModifyUser("system");
        ifCompDeliveryHeader.setAttribute10("1");//已同步

        //创建类型的条件构造器
        QueryWrapper<IfCompDeliveryHeader> wrapper = new QueryWrapper<>();
        wrapper.eq("IF_COMP_DELIVERY_HEADER_ID", data.getIfCompDeliveryHeaderId());

        int row = ifCompDeliveryHeaderMapper.update(ifCompDeliveryHeader, wrapper);
        log.info("主表:成功更新{}条记录",row);
        //2.明细表
        List<Integer> ifCompDeliveryDtlIdList = ifCompDeliveryDtlList.stream().filter(s -> s.getIfCompDeliveryDtlId() != null).map(s -> s.getIfCompDeliveryDtlId()).collect(Collectors.toList());

        IfCompDeliveryDtl ifCompDeliveryDtl = new IfCompDeliveryDtl();
        ifCompDeliveryDtl.setModifyTime(new Date());
        ifCompDeliveryDtl.setModifyUser("system");
        ifCompDeliveryDtl.setAttribute10("1");//已同步

        //创建类型的条件构造器
        QueryWrapper<IfCompDeliveryDtl> wrapperDtl = new QueryWrapper<>();
        wrapperDtl.in("IF_COMP_DELIVERY_DTL_ID",ifCompDeliveryDtlIdList);
        int rowDtl = ifCompDeliveryDtlMapper.update(ifCompDeliveryDtl, wrapperDtl);
        log.info("明细:成功更新{}条记录",rowDtl);


        //检查是否有同步失败的记录，有则更新为成功
        JobSyncErrorData jobSyncErrorData = jobSyncErrorDataMapper.getSyncErrorDataByTypeAndCode(syncType, billNo);
        if (jobSyncErrorData != null && 0 == jobSyncErrorData.getIsSuccess()) {
            syncErrorDataService.updateSuccessByCode(syncType, billNo);
        }
    }

}
